import { Component } from 'react'
import './index.css'

export default class Item extends Component {

  state = {
    mouse: false
  }

  handleMouse(flag) {
    return () => {
      this.setState({
        mouse: flag
      })
    }
  }

  handleChecked = (id, done) => {
    const { updateTodo } = this.props
    updateTodo(id, done)
  }

  // 点击了删除
  handleDelete(id) {
    console.log(id)
    if (window.confirm('确定删除么？')) {
      const { deleteTodo } = this.props
      deleteTodo(id)
    }
  }

  render() {
    const { id, name, done } = this.props
    const { mouse } = this.state

    return (
      <li className="todo-item" onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)} style={{ backgroundColor: mouse ? '#ddd' : '#fff' }}>
        <label>
          <input type="checkbox" checked={done} onChange={(e) => { this.handleChecked(id, e.target.checked) }} />
          <span>{name}</span>
        </label>
        <button onClick={this.handleDelete.bind(this, id)} className="btn btn-danger" style={{ display: mouse?'block':'none' }}>删除</button>
      </li>
    )
  }
}
